  <!-- 内容区域 -->
  <div class="content-wrapper">
    <!-- 内容标题 -->
    <section class="content-header">
        <h1>
            <small>商品管理</small>/添加商品
        </h1>
        <ol class="breadcrumb display-none">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">生成</a></li>
            <li class="active">添加</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content pd-top0">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <div class="box">
                    <div class="box-body solist">
                        <div class="detail-page-sy">
                            <form class="form-horizontal" action="<?=base_url()?>index.php/admin/goods/update/<?= $id?>" method="post" id='EditViewForm'>
                                <div class="detail-page-border">
                                    <h4 class="detail_title">
                                        基本信息
                                    </h4>
                                    <div class="detail_con">
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品主标题</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" placeholder="请输入商品主标题"
												name="goods[name]" fld='name' value="<?php echo (isset($name)?$name:'') ?>">
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品副标题</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" placeholder="请输入商品副标题"
												name="goods[title]" fld='title' value="<?php echo (isset($title)?$title:'') ?>">
                                            </div>
											<div style='color:red;'></div>
                                        </div>
										<?php if('yes' == $is_admin){?>
                                            <div class="form-group hd_title">
                                                <label class="col-sm-2 control-label">品牌：</label>
                                                <div class="col-sm-4">
                                                    <select name="goods[brand_id]" fld='brand_id' style='width:150px;' class='form-control'>
                                                        <option value="">请选择</option>
                                                        <?php
                                                        foreach ($brand_opts as $key => $opts) {
                                                        ?>
                                                            <option value="<?=$opts['id'] ?>" <?php if(isset($brand_id) && $brand_id == $opts['id']) echo 'selected';?>><?=$opts['name'] ?></option>
                                                        <?php
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                                <div style='color:red;'></div>
                                            </div>
                                        <?php }?>
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品分类</label>
                                            <div class="col-sm-2">
                                                <select name="goods[category_first]" fld="category_first" class="form-control"
                                                        seld="<?=$category_first ?>">
                                                    <option value=""></option>
                                                </select>
                                            </div>
                                            <div class="col-sm-2">
                                                <select name="goods[category_parent]" fld="category_parent" class="form-control"
                                                        seld="<?=$category_parent ?>">
                                                    <option value=""></option>
                                                </select>
                                            </div>
                                            <div class="col-sm-2">
                                                <select name="goods[category_id]" fld="category_id" class="form-control"
                                                        seld="<?=$category_id ?>">
                                                    <option value=""></option>
                                                </select>
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品描述</label>
                                            <div class="col-sm-5">
                                                <textarea cols="20" rows="5" name="goods[description]" fld="description" class="form-control"><?php echo (isset($description)?$description:'') ?></textarea>
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品图片1</label>
                                            <div class="col-sm-7">
                                                <div class="cover-area" style="padding: 5px 0px;">
                                                    <div class="oh z cover-hd">
                                                       <iframe src="<?=base_url()?>index.php/admin/goods/upload" class="uploadfile" style="filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0;position:absolute;top:5px;left:15px;cursor: pointer;width:100px;height:100px;" id="imgUpload"></iframe> <a href="javascript:;" class="btn btn-default btn-big">+上传图片</a>
                                                    </div>
                                                    <p class="cover-bd" id="imgArea" style="display: none;">
                                                        <img id="picurl" src=""><a id="delImg" class="vb cover-del" href="javascript:;">删除</a>
                                                    </p>
                                                </div>
												<div style='color:red;' id='img_error_div'></div>
												<div class="box-footer align-center" style='display:none;'>
													<input type="hidden" name="goods[imageurl]" fld='imageurl'
                                                        value="<?php echo (isset($imageurl)?$imageurl:'') ?>">
												</div>
                                            </div>
                                        </div>
										<div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品图片2</label>
                                            <div class="col-sm-7">
                                                <div class="cover-area" style="padding: 5px 0px;">
                                                    <div class="oh z cover-hd">
                                                       <iframe src="<?=base_url()?>index.php/admin/goods/upload2" class="uploadfile" style="filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0;position:absolute;top:5px;left:15px;cursor: pointer;width:100px;height:100px;" id="imgUpload2"></iframe> <a href="javascript:;" class="btn btn-default btn-big">+上传图片</a>
                                                    </div>
                                                    <p class="cover-bd" id="imgArea2" style="display: none;">
                                                        <img id="picurl2" src=""><a id="delImg2" class="vb cover-del" href="javascript:;">删除</a>
                                                    </p>
                                                </div>
												<div style='color:red;' id='img_error_div2'></div>
												<div class="box-footer align-center" style='display:none;'>
													<input type="hidden" name="goods[imageurl2]" fld='imageurl2'
                                                        value="<?php echo (isset($imageurl2)?$imageurl2:'') ?>">
												</div>
                                            </div>
                                        </div>
										<div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品图片3</label>
                                            <div class="col-sm-7">
                                                <div class="cover-area" style="padding: 5px 0px;">
                                                    <div class="oh z cover-hd">
                                                       <iframe src="<?=base_url()?>index.php/admin/goods/upload3" class="uploadfile" style="filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0;position:absolute;top:5px;left:15px;cursor: pointer;width:100px;height:100px;" id="imgUpload3"></iframe> <a href="javascript:;" class="btn btn-default btn-big">+上传图片</a>
                                                    </div>
                                                    <p class="cover-bd" id="imgArea3" style="display: none;">
                                                        <img id="picurl3" src=""><a id="delImg3" class="vb cover-del" href="javascript:;">删除</a>
                                                    </p>
                                                </div>
												<div style='color:red;' id='img_error_div3'></div>
												<div class="box-footer align-center" style='display:none;'>
													<input type="hidden" name="goods[imageurl3]" fld='imageurl3'
                                                        value="<?php echo (isset($imageurl3)?$imageurl3:'') ?>">
												</div>
                                            </div>
                                        </div>
                                    </div>
                                    <h4 class="detail_title">
                                        规格信息
                                    </h4>
                                    <div class="detail_con">
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品规格</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" placeholder="请输入商品规格"
												name="goods[spec]" fld='spec' value="<?php echo (isset($spec)?$spec:'') ?>">
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">商品价格</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" placeholder="请输入商品价格"
												name="goods[price]" fld='price' value="<?php echo (isset($price)?$price:'') ?>">
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">颜色规格</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" placeholder="请输入颜色规格"
												name="goods[colour]" fld='colour' value="<?php echo (isset($colour)?$colour:'') ?>">
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                    </div>
                                    <h4 class="detail_title">
                                        商品详情
                                    </h4>
                                    <div class="detail_con">
                                        <div class="form-group hd_title">
                                            <label class="col-sm-2 control-label">图文详情</label>
                                            <div class="col-sm-8">
                                                <pre style="height: 700px;border: none;" id="editor"></pre>
												<input type="hidden" name="goods[content]" fld='content'>
                                            </div>
											<div style='color:red;'></div>
                                        </div>
                                    </div>
                                    <div class="box-footer align-center">
                                        <button type="button" class="btn btn-primary btn-big bg-pink" onClick="getEditorVlaue()">提交</button>
                                        <a href="<?=base_url()?>index.php/admin/goods/index" class="btn btn-default btn-big">取消</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <!--/.col (left) -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
</div>
<!-- ./wrapper -->
<!-- <script src="../dist/js/demo.js"></script> -->

<script src="<?= base_url()?>/style/js/base/ace/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
	editor.setTheme("ace/theme/twilight");
	editor.session.setMode("ace/mode/html");
	function aceInit(data){console.log(data);
		editor.setValue(data)
	}
    $.post('<?=base_url()?>index.php/admin/goods/postGoodContent/<?= $id?>',{'ajax':1},function(rest){
        if(rest == '') return false;
        aceInit(rest);
    });
</script>

<script type="text/javascript">
    // var pageMenu = {"active": [1]};
    var is_submit = 1;
    function getEditorVlaue(){
        // console.log(editor.getValue());
        var idx = 1;
        $('#EditViewForm input[fld=name]').val($.trim($('#EditViewForm input[fld=name]').val()));
        if($('#EditViewForm input[fld=name]').val() == ''){
            $('#EditViewForm input[fld=name]').parent().next().html('请输入商品主标题。');
            idx = 0;
        }else{
            $('#EditViewForm input[fld=name]').parent().next().html('');
        }
		if($('#EditViewForm input[fld=title]').val() == ''){
            $('#EditViewForm input[fld=title]').parent().next().html('请输入商品副标题。');
            idx = 0;
        }else{
            $('#EditViewForm input[fld=title]').parent().next().html('');
        }
        if($('#EditViewForm select[fld=brand_id]').length > 0){
            if($('#EditViewForm select[fld=brand_id]').val() == ''){
                $('#EditViewForm select[fld=brand_id]').parent().next().html('请选择品牌。');
                idx = 0;
            }else{
                $('#EditViewForm select[fld=brand_id]').parent().next().html('');
            }
        }
        var content = editor.getValue();
        content = $.trim(content);
        if(content == ''){
            $('#editor').parent().next().html('请输入内容。');
            idx = 0;
        }else{
            $('#editor').parent().next().html('');
        }
        if(0 == idx){
            return false;
        }
        $('#EditViewForm input[fld=content]').val(content);
        if(1 != is_submit){
            return false;
        }
        is_submit = 2;
        $('#EditViewForm').submit();
    }
    //删除图片
    $("#delImg").on("click",function(){
        $('.oh').show();
        $('#picurl').attr('src','');
        $('#imgArea').hide();
        $('#EditViewForm input[fld=imageurl]').val('');
    });
	//删除图片
    $("#delImg2").on("click",function(){
        $('.oh').show();
        $('#picurl2').attr('src','');
        $('#imgArea2').hide();
        $('#CreateForm input[fld=imageurl2]').val('');
    });
	//删除图片
    $("#delImg3").on("click",function(){
        $('.oh').show();
        $('#picurl3').attr('src','');
        $('#imgArea3').hide();
        $('#CreateForm input[fld=imageurl3]').val('');
    });

//图片上传
function updataImg(url){
    $('#imgArea').show();
    $('#picurl').attr('src',url);
}
//图片上传
function uploadCallBack(upload_path,upload_file,upload_name,upload_type,upload_size,imageurl){
    $('#EditViewForm input[fld=imageurl]').val(imageurl);
    updataImg(imageurl);
}

//2
//图片上传
function uploadCallBack2(upload_path,upload_file,upload_name,upload_type,upload_size,imageurl){
    $('#EditViewForm input[fld=imageurl2]').val(imageurl);
    updataImg2(imageurl);
}
//图片上传
function updataImg2(url){
    $('#imgArea2').show();
    $('#picurl2').attr('src',url);
}
//3
//图片上传
function uploadCallBack3(upload_path,upload_file,upload_name,upload_type,upload_size,imageurl){
    $('#EditViewForm input[fld=imageurl3]').val(imageurl);
    updataImg3(imageurl);
}
//图片上传
function updataImg3(url){
    $('#imgArea3').show();
    $('#picurl3').attr('src',url);
}

var error_code = "<?= (isset($error_code)?$error_code:'')?>";
if(error_code != ''){
	$('#EditViewForm input[fld=name]').parent().next().html(error_code);
}
var imageurl = $('#EditViewForm input[fld=imageurl]').val();
if(imageurl != '')
	updataImg(imageurl);

var imageurl2 = $('#EditViewForm input[fld=imageurl2]').val();
if(imageurl2 != '')
	updataImg2(imageurl2);

var imageurl3 = $('#EditViewForm input[fld=imageurl3]').val();
if(imageurl3 != '')
	updataImg3(imageurl3);

$(function (){
    //绑定一级分类事件
    //绑定一级分类事件
    $('#EditViewForm select[fld=category_first]').on('change',function(){
        var brand_id = 0;
        if($('#EditViewForm select[fld=brand_id]').length > 0){
            brand_id = $('#EditViewForm select[fld=brand_id]').val();
        }
        $('#EditViewForm select[fld=category_parent]').get(0).length = 1;
        if($(this).val() > 0){
            parent_id = $(this).val();
        }else{
            return false;
        }
		//console.log('parent_id123 : '+parent_id);
        $.post('<?=base_url()?>index.php/admin/category/getCategoryByParent',{'ajax':1,'parent_id':parent_id,'brand_id':brand_id},function(rest){
            if(rest == '') return false;
            var restobj = eval('('+ rest +')')?eval('('+ rest +')'):JSON.parse(rest);
            $.each(restobj.data,function (key,opts){
                $('#EditViewForm select[fld=category_parent]').append('<option value="'+opts.id+'">'+opts.name+'</option>')
            });
            if($('#EditViewForm select[fld=category_parent]').attr('seld') > 0){
                $('#EditViewForm select[fld=category_parent]').val($('#EditViewForm select[fld=category_parent]').attr('seld'));
                $('#EditViewForm select[fld=category_parent]').change();
                $('#EditViewForm select[fld=category_parent]').attr('seld',0);
            }
        });
    });
    //绑定二级分类事件
    $('#EditViewForm select[fld=category_parent]').on('change',function(){
        var brand_id = 0;
        if($('#EditViewForm select[fld=brand_id]').length > 0){
            brand_id = $('#EditViewForm select[fld=brand_id]').val();
        }
        $('#EditViewForm select[fld=category_id]').get(0).length = 1;
        if($(this).val() > 0){
            parent_id = $(this).val();
        }else{
            return false;
        }
        $.post('<?=base_url()?>index.php/admin/category/getCategoryByParent',{'ajax':1,'parent_id':parent_id,'brand_id':brand_id},function(rest){
            if(rest == '') return false;
            var restobj = eval('('+ rest +')')?eval('('+ rest +')'):JSON.parse(rest);
            $.each(restobj.data,function (key,opts){
                $('#EditViewForm select[fld=category_id]').append('<option value="'+opts.id+'">'+opts.name+'</option>')
            });
            if($('#EditViewForm select[fld=category_id]').attr('seld') > 0){
                $('#EditViewForm select[fld=category_id]').val($('#EditViewForm select[fld=category_id]').attr('seld'));
                $('#EditViewForm select[fld=category_id]').attr('seld',0);
            }
        });
    });
});

/**
 * 加载分类事件
 * @author Toby.tu 2017-03-06
 */
function loadCategoryOpts(brand_id=0){
    $.post('<?=base_url()?>index.php/admin/category/getCategoryByParent',{'ajax':1,'parent_id':0,'brand_id':brand_id},function(rest){
        $('#EditViewForm select[fld=category_first]').get(0).length = 1;
        if(rest == '') return false;
        var restobj = eval('('+ rest +')')?eval('('+ rest +')'):JSON.parse(rest);
        $.each(restobj.data,function (key,opts){
            $('#EditViewForm select[fld=category_first]').append('<option value="'+opts.id+'">'+opts.name+'</option>')
        });
		//console.log('first: '+$('#EditViewForm select[fld=category_first]').attr('seld'));
        if($('#EditViewForm select[fld=category_first]').attr('seld') > 0){
			//console.log('first: '+'111111');
           $('#EditViewForm select[fld=category_first]').val($('#EditViewForm select[fld=category_first]').attr('seld'));
            // $('#EditViewForm select[fld=category_first]').trigger('change');
			//console.log("123")
			setTimeout(function(){
				$('#EditViewForm select[fld=category_first]').change();
			},300);
			$('#EditViewForm select[fld=category_first]').attr('seld',0);
        }
    });
}
if($('#EditViewForm select[fld=brand_id]').length > 0){console.log('here 1');
    $('#EditViewForm select[fld=brand_id]').on('change',function(){
		console.log('brand_id : '+$(this).val());
        loadCategoryOpts($(this).val());
    });
    if($('#EditViewForm select[fld=brand_id]').val() > 0){
        $('#EditViewForm select[fld=brand_id]').change();
    }
}else{
    loadCategoryOpts();
}

</script>
<script src="<?= base_url()?>/style/js/vendors.js" type="text/javascript"></script>
